'use strict'

const { TaiSpider } = require('tai-spider');
const { hash } = require('../utils/str-helper');

class SpaceflightnowLaunchSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'spaceflightnow-launch';
        this.debug = true;
        this.start_urls = [
            'https://spaceflightnow.com/launch-log/',
        ];
    }

    *parse(response) {
        let firstPara = response.css('.entry-content p').get(0);
        yield* response.follow_all(firstPara.css('a'))

        for (let ele of response.css('.entry-content .datename')) {
            let missiondata = ele.next('.missiondata').extract().split('\n');

            yield {
                index_name: 'tai-spaceflightnow-launch',
                id: hash(missiondata.join()),
                launchdate: ele.css('.launchdate').extract_first(),
                mission: ele.css('.mission').extract_first(),
                launchtime: missiondata[0].substr('Launch time: '.length),
                launchsite: missiondata[1].substr('Launch site: '.length),
                description: ele.next('.missdescrip').extract(),
                refers: ele.next('.missdescrip').css('a').extract_attr('href'),
            }
        }
    }

}

module.exports = SpaceflightnowLaunchSpider;